package org.suze.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.suze.springboot.base.op.OpResult;
import org.suze.springboot.base.op.OpResultCommon;
import org.suze.springboot.model.UserModel;
import org.suze.springboot.service.UserService;
import org.suze.springboot.utils.RedisGeneral;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @Date : 下午2:49 2017/9/11
 * @Author : 石冬冬-Heil Hitler(dongdong.shi@mljr.com)
 */
@Controller
@RequestMapping("/redis")
public class RedisController {

    private static final String PREFIX = UserModel.class.getSimpleName();
    private static final Logger logger = LoggerFactory.getLogger(RedisController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private RedisGeneral redisGeneral;

    public String getKey(Long id){
        return MessageFormat.format("{0}:{1}",PREFIX,id);
    }

    @RequestMapping(value="insert",method = RequestMethod.POST)
    @ResponseBody
    public OpResult<Boolean> insert(@RequestBody UserModel userModel){
        this.userService.put(userModel,3000);
        return OpResultCommon.suc(Boolean.TRUE);
    }

    @RequestMapping("get/{id}")
    @ResponseBody
    public OpResult<UserModel> get(@PathVariable Long id){
        String key = getKey(id);
        logger.info("show param key={}",key);
        UserModel userModel = (UserModel)this.userService.get(key);
        logger.info("show model ={}",userModel);
        return OpResultCommon.suc(userModel);
    }

    @RequestMapping("del/{id}")
    @ResponseBody
    public OpResult<Boolean> del(@PathVariable Long id){
        String key = getKey(id);
        this.userService.remove(key);
        return OpResultCommon.suc(Boolean.TRUE);
    }

    @RequestMapping("all")
    @ResponseBody
    public OpResult<UserModel> all(){
        return OpResultCommon.suc(this.userService.getAll());
    }

    @RequestMapping("keys")
    @ResponseBody
    public OpResult<UserModel> keys(){
        return OpResultCommon.suc(this.userService.getKeys());
    }

    @RequestMapping("no")
    @ResponseBody
    public OpResult<String> no(){
        return OpResultCommon.suc(this.redisGeneral.general("SK"));
    }

    @RequestMapping("nos")
    @ResponseBody
    public OpResult<String> nos(){
        long start = System.currentTimeMillis();
        int capacity = 1000;
        List<String> list = new ArrayList<>(capacity);
        for(int i = 0;i<capacity;i++){
            String no = this.redisGeneral.general("VX");
            list.add(no);
        }
        logger.info("complete general,sec={}",(System.currentTimeMillis() - start));
        return OpResultCommon.suc(list);
    }
}
